<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .title {
        display: flex;
      }
      .title-item {
        display: inline-block;
        padding: 5px 10px;
        margin: 5px;
        cursor: pointer;
        background: lightblue;
        border-radius: 5px;
        color: #000;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .title-item-active {
        background: red;
        color: gold;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <!-- <input v-model="value" /> -->
      <hr />
      <div class="title">
        <span
          class="title-item"
          :class="tabIndex === index? 'title-item-active':''"
          @click="handleTabClick(item, index)"
          v-for="(item, index) in list"
        >
          {{item.name}}
        </span>
      </div>
      <!-- template 是vue的一个虚拟标签，如果不想多写标签的话，可以拿他代替，他不会渲染在页面上 -->
      <!-- <template v-if="list[tabIndex]">
        <ul v-for="item in list[tabIndex].children">
          <li>{{item.name}}</li>
        </ul>
      </template> -->

      <ul v-for="item in list[tabIndex]?.children">
        <li>{{item.name}}</li>
      </ul>
    </div>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="../utils/index.js"></script>
    <script>
      const vm = new Vue({
        el: '#app',
        data() {
          return {
            list: [],
            tabIndex: 0,
          }
        },
        watch: {},
        computed: {},
        // 请求数据
        created() {
          this.getData()
        },
        methods: {
          handleTabClick(item, index) {
            this.tabIndex = index
          },
          async getData() {
            const res = await createData()
            this.list = res.data
            console.log('res:', res.data)
            // 数据必须保存在data中，才能渲染在上面的那个#app这个区域中
          },
        },
      })
      function createData() {
        return new Promise((resolve, reject) => {
          resolve({
            code: 0,
            msg: 'success',
            data: [
              {
                id: 1,
                count: 20,
                name: '亚洲',
                children: [
                  {
                    id: '1-1',
                    name: '中国',
                  },
                  {
                    id: '1-2',
                    name: '日本',
                  },
                  {
                    id: '1-3',
                    name: '朝鲜',
                  },
                  {
                    id: '1-4',
                    name: '韩国',
                  },
                  {
                    id: '1-5',
                    name: '蒙古',
                  },
                ],
              },
              {
                id: 2,
                count: 20,
                name: '北美洲',
                children: [
                  {
                    id: '2-1',
                    name: '美国',
                  },
                  {
                    id: '2-2',
                    name: '加拿大',
                  },
                  {
                    id: '2-3',
                    name: '墨西哥',
                  },
                ],
              },
            ],
          })
        })
      }
    </script>
  </body>
</html>
